package com.airS.task.fileMgr.service.gk.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.gk.EmployeeExample;
import com.airS.task.fileMgr.mapper.gk.EmployeeMapper;
import com.airS.task.fileMgr.model.gk.Employee;
import com.airS.task.fileMgr.query.gk.EmployeeQuery;
import com.airS.task.fileMgr.service.gk.EmployeeService;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by alan on 2017/3/14.
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee queryEmployeeById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Employee> queryEmployees(EmployeeQuery query) {
        PageUtil.startPage(query);
        List<Employee> employees = employeeMapper.selectByExample(assemblyEmployeeExample(query));
        return new Page<Employee>(employees);
    }

    @Override
    public Map<Integer, Employee> getAllEmployeeToMap() {
        Map<Integer, Employee> employeeMap = new HashMap<Integer, Employee>();
        for(Employee emp : getAllEmployeeToList()){
            employeeMap.put(emp.getEmployeeId(), emp);
        }
        return employeeMap;
    }

    @Override
    public List<Employee> getAllEmployeeToList(){
        return employeeMapper.selectByExample(new EmployeeExample());
    }

    @Override
    public Integer addEmployee(Employee employee) {
        employeeMapper.insert(employee);
        return employee.getEmployeeId();
    }

    @Override
    public Integer updateEmployeeById(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public Integer delEmployeeById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delEmployeeByIds(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmployeeIdIn(ids);
        return employeeMapper.deleteByExample(example);
    }

    private EmployeeExample assemblyEmployeeExample(EmployeeQuery query){
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        // 登入
        if(StringUtils.hasText(query.getAccount())){
            criteria.andAccountEqualTo(query.getAccount());
        }
        if(StringUtils.hasText(query.getEmployeeName())){
            criteria.andEmployeeNameLike("%" + query.getEmployeeName() + "%");
        }
        if(StringUtils.hasText(query.getGroupName())){
            criteria.andGroupNameLike("%" + query.getGroupName() + "%");
        }
        if(StringUtils.hasText(query.getOrganName())){
            criteria.andOrganNameLike("%" + query.getOrganName() + "%");
        }
        if(StringUtils.hasText(query.getType())){
            criteria.andTypeEqualTo(query.getType());
        }
        example.setOrderByClause("create_time desc");
        return example;
    }
}
